﻿using System;
using System.Collections.Generic;

namespace FrameOperateSync
{
    public enum NetMode
    { 
        localTest,
        network,
    }
    class FrameNetwork
    {
        private RenderLogicManager _render;
        private OperateLogicManager _operate;
        private NetMode _netMode;
       // private event Action
        public FrameNetwork(NetMode mode, OperateLogicManager operateLogic, RenderLogicManager renderLogic)
        {
            _operate = operateLogic;
            _render = renderLogic;
            _netMode = mode;
            operateLogic.EventKeyFrameOperatesCollect += OnKeyFrameDataCollect;
        }
        private void OnKeyFrameDataCollect(bool flag, int keyFrameIndex, KeyValuePair<int, FrameOperateData>[] datas)
        {
            if(_netMode == NetMode.localTest)
            {
                OnLocalKeyFrameDataCollect(flag, keyFrameIndex, datas);
            }
            else
            {
                OnNetworkKeyFrameDataCollcet(flag, keyFrameIndex, datas);
            }
        }

        private void OnLocalKeyFrameDataCollect(bool flag, int keyFrameIndex, KeyValuePair<int, FrameOperateData>[] datas)
        {
            if (datas == null || datas.Length == 0)
            {
                _render.RecvRenderData(FrameOperateData.Empty(keyFrameIndex));
                return;
            }
            Loger.Log($"OnLocalFrameDataCollect:{flag},{keyFrameIndex},{datas.Length}");
            foreach(var data in datas)
            {
                Loger.LogWarning("111223");
               var frameData = FrameOperateData.Create(_operate.PlayerId, keyFrameIndex, data.Key, data.Value.LogicCode, data.Value.Data);
                _render.RecvRenderData(frameData);
            }

        }
        private void OnNetworkKeyFrameDataCollcet(bool flag, int keyFrameIndex, KeyValuePair<int, FrameOperateData>[] datas)
        {

        }

    }
}
